if ("serviceWorker" in navigator) {
    log("Service Worker is supported");
    navigator.serviceWorker.register("sw.js").then(function (reg) {
        log(":^)");
        return reg.pushManager.subscribe({
            userVisibleOnly: true
        }).then(function (sub) {
            var req = new XMLHttpRequest();
            req.open("post", "/subscription");
            req.send(sub.endpoint);
        });
    }).catch(function (err) {
        log(err);
    });
}

function log (message) {
    document.getElementById("log").textContent = message;
};